#!/bin/bash
##K8S初始化脚本
check_status(){
    if [[ "$1" == '0' ]];then
        echo -e "${2}成功"
    else
        echo -e "${2}失败"
    fi
}
ver=`kubeadm version|awk '{print $5}'|sed "s/[^0-9|\.]//g"|awk 'NR==1{print}'`
swapoff -a
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
kubeadm init --kubernetes-version=${ver}  \
--apiserver-advertise-address=HOST_IP   \
--image-repository registry.aliyuncs.com/google_containers  \
--service-cidr=10.10.0.0/16 \
--pod-network-cidr=10.122.0.0/16
check_status "$?" 'K8S初始化成功'
mkdir -p ${HOME}/.kube
cp -i /etc/kubernetes/admin.conf ${HOME}/.kube/config
chown $(id -u):$(id -g) ${HOME}/.kube/config
kubectl apply -f /init/calico.yaml
check_status "$?" '[calico.yaml]文件应用'
# source /usr/share/bash-completion/bash_completion
# source <(kubectl completion bash)
# kubeadm token create --print-join-command
# check_status "$?" "注册URL生成"
status='true'
while $status;do
	kube_status=`kubectl get node | awk '{print $2}' | sed -n 2p`
	if [[ "$kube_status" -eq 'Ready' ]];then
        status='false'
		echo 'Master节点初始化完成,请在Node节点执行下面的命令加入本节点'
        connect=`kubeadm token create --print-join-command`
        echo $connect >> /images_master_pull.sh
        echo $connect
	else
		status='true'
		echo '继续等待'
        for((i=3;i=1;i--));do
            sleep 1
            echo -e "检测倒计时: [ $i ]秒"
        done
	fi

done